#include "sdk.h"
#include <map>
#include <wx/arrstr.h>
#include "filefilters.h"
#include "globals.h"

typedef std::map<wxString, wxString> FileFiltersMap;
static FileFiltersMap s_Filters;
static size_t s_LastFilterAllIndex = 0;

void FileFilters::AddDefaultFileFilters() {
  if( s_Filters.size() != 0 ) {
    return;
  }
  Add( _T( "工作空间" ), _T( "*.m_workspace" ) );
  Add( _T( "项目" ), _T( "*.m_pr" ) );
  Add( _T( "项目/工作空间" ), _T( "*.m_workspace;*.m_pr" ) );
}

bool FileFilters::Add( const wxString& name, const wxString& mask ) {
  if( name.IsEmpty() || mask.IsEmpty() ) {
    return false;
  }
  if( mask.Index( _T( ',' ) ) != wxString::npos ) {
    wxString tmp = mask;
    while( tmp.Replace( _T( "," ), _T( ";" ) ) ) ;
    s_Filters[name] = tmp;
  } else {
    s_Filters[name] = mask;
  }
  return true;
}

wxString FileFilters::GetFilterString( const wxString& ext ) {
  size_t count = 0;
  wxString ret;
  for( FileFiltersMap::iterator it = s_Filters.begin(); it != s_Filters.end(); ++it ) {
    if( !ext.IsEmpty() ) {
      bool match = false;
      wxArrayString array = GetArrayFromString( it->second, _T( ";" ), true );
      for( size_t i = 0; i < array.GetCount(); ++i ) {
        if( ext.Matches( array[i] ) ) {
          match = true;
          break;
        }
      }
      if( !match ) {
        continue;
      }
    }
    ++count;
    if( !ret.IsEmpty() ) {
      ret << _T( '|' );
    }
    ret << it->first << _T( '|' ) << it->second;
  }
  if( !ret.IsEmpty() ) {
    ret << _T( '|' );
  }
  ret << GetFilterAll();
  s_LastFilterAllIndex = count;
  return ret;
}

wxString FileFilters::GetFilterAll() {
  s_LastFilterAllIndex = 0;
  return _T( "所有文件 (*)|*" );
}

size_t FileFilters::GetIndexForFilterAll() {
  return s_LastFilterAllIndex;
}

bool FileFilters::GetFilterIndexFromName( const wxString& FiltersList, const wxString& FilterName, int& Index ) {
  bool bFound = false;
  wxArrayString List = GetArrayFromString( FiltersList, _T( "|" ), true );
  int LoopEnd = static_cast<int>( List.GetCount() );
  for( int idxList = 0; idxList < LoopEnd; idxList += 2 ) {
    if( List[idxList] == FilterName ) {
      Index = idxList / 2;
      bFound = true;
      break;
    }
  }
  return bFound;
}

bool FileFilters::GetFilterNameFromIndex( const wxString& FiltersList, int Index, wxString& FilterName ) {
  bool bFound = false;
  wxArrayString List = GetArrayFromString( FiltersList, _T( "|" ), true );
  int LoopEnd = static_cast<int>( List.GetCount() );
  if( 2 * Index < LoopEnd ) {
    FilterName = List[2 * Index];
    bFound = true;
  }
  return bFound;
}

const wxString FileFilters::M_WORKSPACE_EXT = _T( "m_workspace" );
const wxString FileFilters::M_CODE_EXT = _T( "m_pr" );
const wxString FileFilters::ASM_EXT = _T( "asm" );
const wxString FileFilters::C_EXT = _T( "c" );
const wxString FileFilters::CC_EXT = _T( "cc" );
const wxString FileFilters::CPP_EXT = _T( "cpp" );
const wxString FileFilters::CXX_EXT = _T( "cxx" );
const wxString FileFilters::TPP_EXT = _T( "tpp" );
const wxString FileFilters::INL_EXT = _T( "inl" );
const wxString FileFilters::H_EXT = _T( "h" );
const wxString FileFilters::HH_EXT = _T( "hh" );
const wxString FileFilters::HPP_EXT = _T( "hpp" );
const wxString FileFilters::HXX_EXT = _T( "hxx" );
const wxString FileFilters::S_EXT = _T( "s" );
const wxString FileFilters::SS_EXT = _T( "ss" );
const wxString FileFilters::S62_EXT = _T( "s62" );
const wxString FileFilters::OBJECT_EXT = _T( "o" );
const wxString FileFilters::XRCRESOURCE_EXT = _T( "xrc" );
const wxString FileFilters::STATICLIB_EXT = _T( "a" );
const wxString FileFilters::RESOURCE_EXT = _T( "rc" );
const wxString FileFilters::RESOURCEBIN_EXT = _T( "res" );
const wxString FileFilters::XML_EXT = _T( "xml" );
const wxString FileFilters::SCRIPT_EXT = _T( "script" );
const wxString FileFilters::DYNAMICLIB_EXT = _T( "so" );
const wxString FileFilters::EXECUTABLE_EXT = _T( "" );
const wxString FileFilters::NATIVE_EXT = _T( "ko" );

const wxString FileFilters::M_WORKSPACE_DOT_EXT = _T( '.' ) + FileFilters::M_WORKSPACE_EXT;
const wxString FileFilters::M_CODE_DOT_EXT = _T( '.' ) + FileFilters::M_CODE_EXT;
const wxString FileFilters::ASM_DOT_EXT = _T( '.' ) + FileFilters::ASM_EXT;
const wxString FileFilters::C_DOT_EXT = _T( '.' ) + FileFilters::C_EXT;
const wxString FileFilters::CC_DOT_EXT = _T( '.' ) + FileFilters::CC_EXT;
const wxString FileFilters::CPP_DOT_EXT = _T( '.' ) + FileFilters::CPP_EXT;
const wxString FileFilters::CXX_DOT_EXT = _T( '.' ) + FileFilters::CXX_EXT;
const wxString FileFilters::TPP_DOT_EXT = _T( '.' ) + FileFilters::TPP_EXT;
const wxString FileFilters::INL_DOT_EXT = _T( '.' ) + FileFilters::INL_EXT;
const wxString FileFilters::H_DOT_EXT = _T( '.' ) + FileFilters::H_EXT;
const wxString FileFilters::HH_DOT_EXT = _T( '.' ) + FileFilters::HH_EXT;
const wxString FileFilters::HPP_DOT_EXT = _T( '.' ) + FileFilters::HPP_EXT;
const wxString FileFilters::HXX_DOT_EXT = _T( '.' ) + FileFilters::HXX_EXT;
const wxString FileFilters::S_DOT_EXT = _T( '.' ) + FileFilters::S_EXT;
const wxString FileFilters::SS_DOT_EXT = _T( '.' ) + FileFilters::SS_EXT;
const wxString FileFilters::S62_DOT_EXT = _T( '.' ) + FileFilters::S62_EXT;
const wxString FileFilters::OBJECT_DOT_EXT = _T( '.' ) + FileFilters::OBJECT_EXT;
const wxString FileFilters::XRCRESOURCE_DOT_EXT = _T( '.' ) + FileFilters::XRCRESOURCE_EXT;
const wxString FileFilters::STATICLIB_DOT_EXT = _T( '.' ) + FileFilters::STATICLIB_EXT;
const wxString FileFilters::RESOURCE_DOT_EXT = _T( '.' ) + FileFilters::RESOURCE_EXT;
const wxString FileFilters::RESOURCEBIN_DOT_EXT = _T( '.' ) + FileFilters::RESOURCEBIN_EXT;
const wxString FileFilters::XML_DOT_EXT = _T( '.' ) + FileFilters::XML_EXT;
const wxString FileFilters::SCRIPT_DOT_EXT = _T( '.' ) + FileFilters::SCRIPT_EXT;
const wxString FileFilters::DYNAMICLIB_DOT_EXT = _T( '.' ) + FileFilters::DYNAMICLIB_EXT;
const wxString FileFilters::EXECUTABLE_DOT_EXT = EXECUTABLE_EXT;
const wxString FileFilters::NATIVE_DOT_EXT = NATIVE_EXT;
